package com.petsbang.action;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.petsbang.action.dao.DataApplication;
import com.petsbang.common.Common;
import com.petsbang.http.DataHandler;
import com.petsbang.http.DataThread;
import com.petsbang.ui.LazyScrollView;
import com.petsbang.ui.LazyScrollView.OnScrollListener;
import com.petsbang.utils.ImageLoaderTask;
import com.petsbang.vo.PictureInfo;
import com.petsbang.vo.TaskParam;

public class PetsMainActivity extends Activity {

	
	private LazyScrollView waterfall_scroll;
	private LinearLayout waterfall_container;
	private ArrayList<LinearLayout> waterfall_items;
	private Display display;

	private int itemWidth;
	private int column_count = 2;// 显示列数
	private int page_count = 14; // 每次加载15张图片
	private int leftHeight;
	private int rightHeight;
	private int current_page = 0;

	private DataThread thread;
	public DataHandler dh;
	public DataApplication appState;
	public ProgressBar loadDataBar;
	
	private String pictureTag;
	private String serverUrl;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		VMRuntime.getRuntime().setTargetHeapUtilization(Common.TARGET_HEAP_UTILIZATION);
//		VMRuntime.getRuntime().setMinimumHeapSize(Common.CWJ_HEAP_SIZE);
		
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		pictureTag = bundle.getString("PictureTag");
		serverUrl = bundle.getString("ServerUrl");	
		// 初始化界面
		initView();
		// 下载图片相关信息
		startDataThread();

	}

	public void initView() {
		appState = (DataApplication) getApplicationContext();
		
		if (appState.getPicList() != null) {
			appState.setPicList(null);
		}

		display = this.getWindowManager().getDefaultDisplay();
		appState.setWidth(display.getWidth());
		appState.setHeight(display.getHeight());
		// 根据屏幕大小计算每列大小
		itemWidth = display.getWidth() / column_count;
		setContentView(R.layout.main);
		
		TextView title = (TextView) findViewById(R.id.title);
		if (pictureTag.equals(Common.TAG_HEAVY)) {
			title.setText(R.string.funny);
		} else if(pictureTag.equals(Common.TAG_MENG)){
			title.setText(R.string.meng);
		} else if(pictureTag.equals(Common.TAG_CARTON)){
			title.setText(R.string.carton);
		} else if(pictureTag.equals(Common.TAG_START)){
			title.setText(R.string.supper_start);
		}

		loadDataBar = (ProgressBar) findViewById(R.id.loadDataBar);
	}

	void startDataThread() {
		thread = new DataThread(PetsMainActivity.this, serverUrl,Common.pic_type,pictureTag);
		thread.start();
	}

	public void InitLayout() {
		loadDataBar.setVisibility(View.INVISIBLE);

		waterfall_scroll = (LazyScrollView) findViewById(R.id.waterfall_scroll);
		waterfall_scroll.getView();
		waterfall_scroll.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onTop() {
				// 滚动到最顶端
				Log.d("LazyScroll", "Scroll to top");
			}

			@Override
			public void onScroll() {
				// 滚动中
				Log.d("LazyScroll", "Scroll");
			}

			@Override
			public void onBottom() {
				// 滚动到最低端
				AddItemToContainer(++current_page, page_count);
			}
		});

		waterfall_container = (LinearLayout) this
				.findViewById(R.id.waterfall_container);
		waterfall_items = new ArrayList<LinearLayout>();

		for (int i = 0; i < column_count; i++) {
			LinearLayout itemLayout = new LinearLayout(this);
			LinearLayout.LayoutParams itemParam = new LinearLayout.LayoutParams(
					itemWidth, LayoutParams.WRAP_CONTENT);
			itemLayout.setPadding(4, 4, 4, 4);
			itemLayout.setOrientation(LinearLayout.VERTICAL);

			itemLayout.setLayoutParams(itemParam);
			waterfall_items.add(itemLayout);
			waterfall_container.addView(itemLayout);
		}

		// 第一次加载
		AddItemToContainer(current_page, page_count);
	}

	private void AddItemToContainer(int pageindex, int pagecount) {
		int j = 0;
		// 加载所有图片路径
		ArrayList<PictureInfo> picList = appState.getPicList();
		if (picList == null) {
			return;
		}
		int count = picList.size();
		for (int i = pageindex * pagecount; i < pagecount * (pageindex + 1)
				&& i < count; i++) {
			j = j >= column_count ? j = 0 : j;
			AddImage(picList.get(i), j++);
		}
	}

	private void AddImage(final PictureInfo pic, int columnIndex) {
		
		LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(this)
				.inflate(R.layout.item_for_flow_list, null);
		LinearLayout.LayoutParams itemParam = new LinearLayout.LayoutParams(
				itemWidth - 8, LayoutParams.WRAP_CONTENT);
		int imageHeight = pic.getImage_height() + 57;
		itemParam.height = (imageHeight);
		itemParam.bottomMargin = 6;
		
		linearLayout.setLayoutParams(itemParam);

		linearLayout.setOnClickListener(new LinearLayout.OnClickListener() {
			public void onClick(View v) {
				ArrayList<PictureInfo> picList = appState.getPicList();
				int index = picList.indexOf(pic);

				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putInt("index", index);
				bundle.putString("PictureTag", pictureTag);
				intent.putExtras(bundle);
				intent.setClass(PetsMainActivity.this, LovelyActivity.class);

				startActivity(intent);
			}
		});

		if (leftHeight >= rightHeight) {
			columnIndex = 0;
			rightHeight += imageHeight;
		} else {
			columnIndex = 1;
			leftHeight += imageHeight;
		}
		waterfall_items.get(columnIndex).addView(linearLayout);

		ImageView imageView = (ImageView) linearLayout
				.findViewById(R.id.imageview);

		String imageUrl = pic.getLarge_url();
		TaskParam param = new TaskParam();
		param.setFilename(imageUrl);
		param.setItemWidth(itemWidth);
		ImageLoaderTask task = new ImageLoaderTask(imageView);
		task.execute(param);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 清除信息
		appState.setPicList(null);
	}


}